package tt.dz.biz.service.account;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.util.CollectionUtils;
import tt.dz.biz.dao.account.AccountingDAO;
import tt.dz.common.BigDecimalUtil;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.balType;
import tt.dz.common.DzConstants.balanceFlag;
import tt.dz.common.DzConstants.borrowOrLoanFlag;
import tt.dz.common.DzConstants.receiveOrPay;
import tt.dz.common.DzConstants.subjectType;
import tt.dz.common.LogUtil;
import tt.dz.vo.AccountDetailVO;
import tt.dz.vo.AccountingRuleVO;
import tt.dz.vo.MerchAccountDividendVO;
import tt.dz.vo.MerchantSubjectAccVO;
import tt.dz.vo.SubAccountingRuleVO;
import tt.dz.vo.TranInfoVO;
import tt.dz.vo.TranSubVO;

public class MerchantToIndividualPOJO
  extends AccountDividendPOJO
{
  public MerchantToIndividualPOJO() {}
  
  public MerchantToIndividualPOJO(TranInfoVO tranInfoVO)
    throws Exception
  {
    super(tranInfoVO);
    init();
  }
  
  public MerchantToIndividualPOJO init()
    throws Exception
  {
    try
    {
      this.platMerchantCode = this.tranInfoVO.getOriginalPayer();
    }
    catch (Exception e)
    {
      LogUtil.error("初始化交易记录失败", e);
      throw e;
    }
    return this;
  }
  
  private MerchantToIndividualPOJO addTotalaccount()
    throws Exception
  {
    try
    {
      MerchAccountDividendVO detail = new MerchAccountDividendVO();
      MerchantSubjectAccVO mertchSubject = new MerchantSubjectAccVO();
      mertchSubject.setMerchantCode(detail.getMerchantCode());
      mertchSubject.setSubSubjectCode(detail.getSubjectCode());
      if (this.isRealTime)
      {
        detail.setSubSubjectCode(this.bankSubjectCode);
        detail.setLoanFlag(DzConstants.borrowOrLoanFlag.LOAN_FLAG.value());
        detail.setAmount(this.tranInfoVO.getPaymentAmount());
        mertchSubject.setAcctBal(BigDecimalUtil.sub(BigDecimal.valueOf(0L), detail.getAmount()));
      }
      else
      {
        AccountingRuleVO rule = this.accountingDAO.queryAssetsAccountRuleByServiceNo(this.tranInfoVO.getServiceNo(), DzConstants.subjectType.LIABILITIES.value(), this.tranInfoVO.getPayTypeCode());
        detail.setSubjectCode(rule.getSubjectCode());
        detail.setSubSubjectCode(rule.getSubSubjectCode());
        detail.setLoanFlag(rule.getLoanFlag());
        detail.setAmount(this.tranInfoVO.getPaymentAmount());
        if (DzConstants.balanceFlag.REDUCTION.value().equals(rule.getBalanceFlag())) {
          mertchSubject.setAcctBal(BigDecimalUtil.sub(BigDecimal.valueOf(0L), detail.getAmount()));
        } else {
          mertchSubject.setAcctBal(detail.getAmount());
        }
      }
      detail.setTradeNo(this.tranInfoVO.getTradeNo());
      detail.setMerchantCode(this.platMerchantCode);
      detail.setMerchantCode1(((TranSubVO)this.tranSubVOList.get(0)).getReceiver());
      detail.setAccountingDate(this.accountingDate);
      this.merchAccountDividendList.add(detail);
      this.merchantSubjectList.add(mertchSubject);
    }
    catch (Exception e)
    {
      LogUtil.error("处理总账户异常", e);
      throw e;
    }
    return this;
  }
  
  private MerchantToIndividualPOJO addTranSubToAccountDetailList()
    throws Exception
  {
    try
    {
    	AccountingRuleVO   rule = null;
    	SubAccountingRuleVO  subRule = null;
      MerchAccountDividendVO  detail = null;
      AccountDetailVO   personlAccountDetail = null;
      Integer balType = Integer.valueOf(0);
      if (this.isRealTime) {
        balType = DzConstants.balType.REAL_TIME.value();
      } else {
        balType = DzConstants.balType.PERIODIC_CLEARING.value();
      }
      if (!CollectionUtils.isEmpty(this.merchantTranSubList)) {
        for (TranSubVO tmp : this.merchantTranSubList)
        {
          rule = new AccountingRuleVO();
          rule.setSubServiceNo(tmp.getServiceNo());
          rule.setServiceNo(this.tranInfoVO.getServiceNo());
          
          rule = this.accountingDAO.queryRecieverAccountingRule(rule);
          personlAccountDetail = new AccountDetailVO();
          personlAccountDetail.setAccountingDate(this.accountingDate);
          personlAccountDetail.setSeqNo(queryIndividualDetailSeq());
          personlAccountDetail.setUserId(tmp.getReceiver());
          personlAccountDetail.setMerchantCode(this.tranInfoVO.getOriginalPayer());
          personlAccountDetail.setPayFlag(DzConstants.receiveOrPay.RECEIVE.value());
          personlAccountDetail.setPlatTime(this.platTime);
          personlAccountDetail.setOrderNo(this.tranInfoVO.getOrderNo());
          personlAccountDetail.setTradeNo(this.tranInfoVO.getTradeNo());
          personlAccountDetail.setServiceNo(tmp.getServiceNo());
          personlAccountDetail.setPayTypeCode(this.tranInfoVO.getPayTypeCode());
          personlAccountDetail.setThdPaySeq(this.tranInfoVO.getThdPaySeq());
          personlAccountDetail.setPaymentAmount(tmp.getPaymentAmount());
          this.personalAccountDetailList.add(personlAccountDetail);
          
          subRule = this.accountingDAO.querySubAccountingRuleByRuleSeq(rule.getRuleSeq());
          detail = new MerchAccountDividendVO();
          detail.setMerchantCode(this.platMerchantCode);
          detail.setAccountingDate(this.accountingDate);
          detail.setMerchantCode1(tmp.getReceiver());
          detail.setAmount(tmp.getPaymentAmount());
          detail.setSubjectCode(subRule.getSubjectCode());
          detail.setSubSubjectCode(subRule.getSubSubjectCode());
          detail.setLoanFlag(subRule.getLoanFlag());
          detail.setTradeNo(this.tranInfoVO.getTradeNo());
          setMerchantSubject(detail, subRule.getBalanceFlag());
          this.merchAccountDividendList.add(detail);
        }
      }
    }
    catch (Exception e)
    {
      AccountingRuleVO rule;
      SubAccountingRuleVO subRule;
      MerchAccountDividendVO detail;
      AccountDetailVO personlAccountDetail;
      LogUtil.error("交易分录转换成运营商账户分录失败,参数为" + this.merchantTranSubList, e);
      throw e;
    }
    return this;
  }
  
  public void saveAccountDividend()
    throws Exception
  {
    init().addTotalaccount().addTranSubToAccountDetailList();
    
    this.merchAccountDividendList = mergeMerchAccountDividend(this.merchAccountDividendList);
    this.accountingDAO.insertMerchAccountDividend(this.merchAccountDividendList);
    this.accountingDAO.insertAccountDetail(this.personalAccountDetailList);
  }
}
